package cn.huobi.basic.util;

import cn.huobi.basic.jwt.*;
import cn.huobi.user.domain.Logininfo;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.security.PublicKey;

@Slf4j
public class LoginContext {

    public static Logininfo getLoginContext(HttpServletRequest request){
        // 获取请求头
        String token = request.getHeader("token");
        // 获取公钥
        PublicKey publicKey = RsaUtils.getPublicKey(JwtRasHolder.INSTANCE.getJwtRsaPubData());
        // 公钥解密
        Payload<UserInfo> payload = JwtUtils.getInfoFromToken(token, publicKey, UserInfo.class);
        // 判断用户信息是否存在
        UserInfo userInfo = payload.getUserInfo();
        if (userInfo == null){
            log.error("用户未登录");
            return null;
        }
        return userInfo.getLogininfo();
    }
}
